<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Builddetail extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('builddetail/builddetail_model');
        $this->load->model('common/common_model');
        
        $this->load->library('Myinfo');
	}
    public function index()
	{
	   
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $c = (int)$this->input->get('c');
        if($c!=$user_oasis_orderid)
        {
            $oasisId = $this->common_model->ww_get_map_user_info($userid, $c);
            if($oasisId)
            {
                $this->session->set_userdata('oasis_orderid', $oasisId['oasis_orderid']);
            }
            else
            {
                redirect(site_url('home'));
                die();
            }
            $user_oasis_orderid = $oasisId['oasis_orderid'];
        }
        $data = $this->myinfo->loadUserinfo($userid, $user_oasis_orderid);
        $info_upgrade = $this->builddetail_model->ww_get_user_info_build($userid);

        
        $chk_upgrade = empty($info_upgrade) ? false : true;
        $data['chk_upgrade'] = $chk_upgrade;
        $data['build_v2'] = $this->myinfo->upgrade_build_for_user($userid, $user_oasis_orderid);
        $data['info'] = $info_upgrade;
        
        // header
        
        // content
        $this->load->view('builddetail/builddetail_view', $data);
        
        // footer
    }
    
    function upgrade()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $userid = $this->session->userdata('uid');
        $data = array();
        
        $buildid = (int)$this->input->get('b');
        
        if(!in_array($buildid, array('1','2','3','4','5','6','7','8','9','10','11','12')))
        {
            redirect(site_url('home'));
            die();
        }
        // check res
        $res_upgrade = array();
        $res_upgrade['food'] = '200';
        $res_upgrade['wood'] = '200';
        $res_upgrade['metal'] = '200';
        $res_upgrade['petro'] = '200';
        $time_complete = 100;//demo;
        
        $this->builddetail_model->ww_add_build_upgrade($userid, $buildid, $oasis_orderid, $res_upgrade['food'], $res_upgrade['wood'], $res_upgrade['metal'], $res_upgrade['petro'], $time_complete);
        
        redirect(site_url('build'));
        die();
    }
}